
import _ from "lodash";
import { App } from "vue";
import { CompLoader,ICmpnt, VueComponent } from "xcore/cmpnt";
import $comp from "xcore/cmpnt";
import {emitReady} from "xcore/data"

let loader:any = {designer:null,installed:false,loaded:false}
export default loader

export function getDesigner(callback?:Function):any {
    if (loader.designer) return loader.designer
    const modules = import.meta.glob(["./cmpnts/*.ts", "./confs/*.ts"], { eager: true });
    loader.designer = new CompLoader(modules);
    let uComps = $comp.getAll()
    uComps.forEach(item => {
        loader.designer.addModule(item as ICmpnt)
    })

    loader.designer.install = function (app: App) {
       if(loader.installed) return;
       loader.installed = true;

        // 定义组件加载器类
        const vueCmpnt = import.meta.glob<{ default: VueComponent }>(["./render/*.vue","./cmpnts/*.vue"], { eager: true })
        this.compInst.addComponents(vueCmpnt)
        this.compInst.install(app)
        console.log("组件加载完成:",this.compInst)
        emitReady()
        if(callback)
            callback(loader.designer)
        loader.loaded=true
    }
   

    return loader.designer;
}
